package com.fabriciojf.dao;

import com.fabriciojf.model.GerUpdateConfig;
import com.fabriciojf.persistence.DatabaseLocator;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Classe DAO do gerUpdateConfig
 *
 * @author fabricio
 * @since 03/05/2010
 * @version 1.1 18/05/2010
 */
public class GerUpdateConfigDAO {

    /**
     * Singleton
     */
    private static GerUpdateConfigDAO instance = new GerUpdateConfigDAO();

    public static GerUpdateConfigDAO getInstance() {
        return instance;
    }

    /**
     * Constructor
     */
    private GerUpdateConfigDAO() {
    }

    /**
     * Salva um objeto do tipo gerConfigUpdate
     * 
     * @param gerConfigUpdate objeto
     * @throws java.sql.SQLException
     * @throws java.lang.ClassNotFoundException
     */
    public void save(GerUpdateConfig gerConfigUpdate) throws SQLException, ClassNotFoundException {
        Connection conn = null;
        Statement st = null;

        try {
            conn = DatabaseLocator.getInstance().getConnection();
            st = conn.createStatement();
            st.execute("INSERT INTO ger_update_config (id, atributo, " + " tag, valor)  VALUES ("
                    + gerConfigUpdate.getId() + ",'" + gerConfigUpdate.getAtributo() + "','" + gerConfigUpdate.getTag()
                    + "','" + gerConfigUpdate.getValor() + "')");

        } catch (SQLException e) {
            throw e;
        } finally {
            DatabaseLocator.getInstance().closeResources(conn, st);
        }
    }

    /**
     * Deleta um objeto do tipo gerConfigUpdate
     * 
     * @param gerConfigUpdate objeto
     * @throws java.sql.SQLException
     * @throws java.lang.ClassNotFoundException
     */
    public void delete(GerUpdateConfig gerConfigUpdate) throws SQLException, ClassNotFoundException {
        Connection conn = null;
        Statement st = null;

        try {
            conn = DatabaseLocator.getInstance().getConnection();
            st = conn.createStatement();
            st.execute("DELETE FROM ger_update_config where id = " + gerConfigUpdate.getId());
        } catch (SQLException e) {
            throw e;
        } finally {
            DatabaseLocator.getInstance().closeResources(conn, st);
        }
    }

    /**
     * Retorna um objeto do tipo gerConfigUpdate de acordo com o id passado
     *
     * @param id do objeto desejado
     * @return um objeto gerConfigUpdate
     * @throws java.sql.SQLException
     * @throws java.lang.ClassNotFoundException
     */
    public GerUpdateConfig getGerConfigUpdate(Integer id) throws SQLException, ClassNotFoundException {
        Connection conn = null;
        Statement st = null;
        GerUpdateConfig gerConfigUpdate = new GerUpdateConfig();
        try {
            conn = DatabaseLocator.getInstance().getConnection();
            st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM ger_update_config WHERE id = " + id);
            if (rs.next()) {
                gerConfigUpdate.setId(rs.getInt("id"));
                gerConfigUpdate.setAtributo(rs.getString("atributo"));
                gerConfigUpdate.setTag(rs.getString("tag"));
                gerConfigUpdate.setValor(rs.getString("valor"));
            }
        } catch (SQLException e) {
            throw e;
        } finally {
            DatabaseLocator.getInstance().closeResources(conn, st);
        }
        return gerConfigUpdate;
    }

    /**
     * Retorna um objeto do tipo gerConfigUpdate de acordo com a tag desejada
     *
     * @param tag
     * @return um objeto gerConfigUpdate
     * @throws java.sql.SQLException
     * @throws java.lang.ClassNotFoundException
     */
    public GerUpdateConfig getByTag(String tag) {
        Connection conn = null;
        Statement st = null;
        GerUpdateConfig gerConfigUpdate = new GerUpdateConfig();
        try {
            conn = DatabaseLocator.getInstance().getConnection();

            st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM ger_update_config WHERE tag = '" + tag + "'");
            if (rs.next()) {
                gerConfigUpdate.setId(rs.getInt("id"));
                gerConfigUpdate.setAtributo(rs.getString("atributo"));
                gerConfigUpdate.setTag(rs.getString("tag"));
                gerConfigUpdate.setValor(rs.getString("valor"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } finally {
            DatabaseLocator.getInstance().closeResources(conn, st);
        }
        return gerConfigUpdate;
    }

    /**
     * Lista objetos do tipo gerConfigUpdate
     * 
     * @return
     * @throws java.sql.SQLException
     * @throws java.lang.ClassNotFoundException
     */
    public List<GerUpdateConfig> listarGerConfigUpdate() throws SQLException, ClassNotFoundException {
        Connection conn = null;
        Statement st = null;
        List<GerUpdateConfig> gerConfigUpdates = new ArrayList<GerUpdateConfig>();
        try {
            conn = DatabaseLocator.getInstance().getConnection();
            st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM ger_update_config ORDER BY tag");
            while (rs.next()) {
                GerUpdateConfig gerConfigUpdate = new GerUpdateConfig();

                gerConfigUpdate.setId(rs.getInt("id"));
                gerConfigUpdate.setAtributo(rs.getString("atributo"));
                gerConfigUpdate.setTag(rs.getString("tag"));
                gerConfigUpdate.setValor(rs.getString("valor"));

                gerConfigUpdates.add(gerConfigUpdate);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return gerConfigUpdates;
    }

    /**
     * Retorna se uma determinada tag existe no banco
     * 
     * @param tag
     * @return
     * @throws java.sql.SQLException
     * @throws java.lang.ClassNotFoundException
     */
    public boolean isNull(String tag) {
        Connection conn = null;
        Statement st = null;
        GerUpdateConfig gerConfigUpdate = new GerUpdateConfig();
        try {
            conn = DatabaseLocator.getInstance().getConnection();
            st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM ger_update_config WHERE tag = '" + tag + "'");

            return !rs.next();

        } catch (SQLException e) {
            e.printStackTrace();
            return true;
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
            return true;
        } finally {
            DatabaseLocator.getInstance().closeResources(conn, st);
        }
    }
}
